package com.hxd.mall.utils.open

import android.annotation.SuppressLint
import java.lang.Exception
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by qishuai on 2018/8/14.
 */
@SuppressLint("SimpleDateFormat")
object TimeUtil {
    var format1: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var format2: DateFormat = SimpleDateFormat("MM-dd")
    var format3: DateFormat = SimpleDateFormat("MM.dd")
    var matchFormat: DateFormat = SimpleDateFormat("yyyy.MM.dd")
    var matchListFormat: DateFormat = SimpleDateFormat("yyyy/MM/dd")
    var format4: DateFormat = SimpleDateFormat("yyyy-MM-dd")
    var format5: DateFormat = SimpleDateFormat("yyyyMMddHHmmss")
    var format6: DateFormat = SimpleDateFormat("yyyyMMdd")
    var format7: DateFormat = SimpleDateFormat("yyyy-MM")
    var format8: DateFormat = SimpleDateFormat("MM-dd HH:mm")
    var format10: DateFormat = SimpleDateFormat("M月dd日")
    var format11: DateFormat = SimpleDateFormat("yyMMdd")
    var format12: DateFormat = SimpleDateFormat("yyMMddHHmm")
    var format13: DateFormat = SimpleDateFormat("M/d")
    var format14: DateFormat = SimpleDateFormat("y年M月")
    var format15: DateFormat = SimpleDateFormat("HH")
    var format16: DateFormat = SimpleDateFormat("HH:mm")
    var format17: DateFormat = SimpleDateFormat("y年M月d日")


    fun getNowDataStr(): String {
        return format1.format(Date())
    }

    /**
     * 秒数
     */
    fun getNowDataStr(time: Long): String {
        return format1.format(Date(time * 1000))
    }

    /**
     * 将秒数转为时分秒
     * 3:3:3
     */
    fun secondConvertToHMS(second: Int): String {
        var h = 0
        var d = 0
        var s = 0
        val temp = second % 3600
        if (second > 3600) {
            h = second / 3600
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60
                    if (temp % 60 != 0) {
                        s = temp % 60
                    }
                } else {
                    s = temp
                }
            }
        } else {
            d = second / 60
            if (second % 60 != 0) {
                s = second % 60
            }
        }

        return h.toString() + ":" + d + ":" + s + ""
    }

    /**
     *
     * 将秒数转为时分秒
     * 3时3分3秒
     */
    fun secondConvertToHMSForChinese(second: Int): String {
        var h = 0
        var d = 0
        var s = 0
        try {
            val temp = second % 3600
            if (second > 3600) {
                h = second / 3600
                if (temp != 0) {
                    if (temp > 60) {
                        d = temp / 60
                        if (temp % 60 != 0) {
                            s = temp % 60
                        }
                    } else {
                        s = temp
                    }
                }
            } else {
                d = second / 60
                if (second % 60 != 0) {
                    s = second % 60
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return h.toString() + "时" + d + "分" + s + "秒"
    }

    /**
     * 根据时间戳获取特定格式日期
     *
     * 这里在有些手机上获取到的时间格式化后成为：2019-0004-18 0013:19:21
     * 所以这里特意排除一下
     */
    fun getTimeFormat1ByDate(date: Long): String {
        val format = format1.format(date)
        try {
            // "yyyy-MM-dd HH:mm:ss"
            val split = format.split(" ")
            val start = split[0].split("-")
            val end = split[1].split(":")

            var timeContent = ""
            start.forEach {
                val item = it.toInt()
                timeContent = if (item < 10) {
                    "${timeContent}0$item-"
                } else {
                    "$timeContent$item-"
                }
            }
            timeContent = "${timeContent.substring(0, timeContent.length - 1)} "

            end.forEach {
                val item = it.toInt()
                timeContent = if (item < 10) {
                    "${timeContent}0$item:"
                } else {
                    "$timeContent$item:"
                }
            }
            return timeContent.substring(0, timeContent.length - 1)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return format
    }
}